package cn.chenzd.easyrecord.db;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import cn.chenzd.easyrecord.entity.Images;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "IMAGES".
*/
public class ImagesDao extends AbstractDao<Images, Long> {

    public static final String TABLENAME = "IMAGES";

    /**
     * Properties of entity Images.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property AccountId = new Property(1, Long.class, "accountId", false, "ACCOUNT_ID");
        public final static Property Path = new Property(2, String.class, "path", false, "PATH");
    }

    private Query<Images> account_ImagesQuery;

    public ImagesDao(DaoConfig config) {
        super(config);
    }
    
    public ImagesDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"IMAGES\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"ACCOUNT_ID\" INTEGER," + // 1: accountId
                "\"PATH\" TEXT NOT NULL );"); // 2: path
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"IMAGES\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Images entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long accountId = entity.getAccountId();
        if (accountId != null) {
            stmt.bindLong(2, accountId);
        }
        stmt.bindString(3, entity.getPath());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Images entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long accountId = entity.getAccountId();
        if (accountId != null) {
            stmt.bindLong(2, accountId);
        }
        stmt.bindString(3, entity.getPath());
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public Images readEntity(Cursor cursor, int offset) {
        Images entity = new Images( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // accountId
            cursor.getString(offset + 2) // path
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, Images entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setAccountId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setPath(cursor.getString(offset + 2));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(Images entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(Images entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Images entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "images" to-many relationship of Account. */
    public List<Images> _queryAccount_Images(Long accountId) {
        synchronized (this) {
            if (account_ImagesQuery == null) {
                QueryBuilder<Images> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.AccountId.eq(null));
                account_ImagesQuery = queryBuilder.build();
            }
        }
        Query<Images> query = account_ImagesQuery.forCurrentThread();
        query.setParameter(0, accountId);
        return query.list();
    }

}
